package org.ccay.demo.student.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.BatchVO;
import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.ioc.ISelfInject;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.util.DaoUtil;
import org.ccay.demo.student.StudentVO;
import org.ccay.demo.student.dao.IStudentDao;
import org.ccay.demo.student.enumerable.HobbyEnum;
import org.ccay.demo.student.service.IStudentService;
import org.ccay.enumerable.EnumItem;

/**
 * 测试--学生service实现
 */
@Named
public class StudentService implements IStudentService, ISelfInject {

	private static final String MESSAGE = "Student";
	private static final ILogger logger = CcayLoggerFactory.getLogger(StudentService.class);
	@Inject
	private IStudentDao studentDao;
	private IStudentService self;

	/**
	 * 分页查找学生信息
	 * @param queryStudent 查询条件
	 * @param pageVO 分页信息
	 * @return
	 */
	public PagedResult<StudentVO> findPagedStudentListFilter(StudentVO queryStudent, PageVO pageVO) {
		logger.debug("Before findPagedStudentList");
		logger.debug("CurPage: " + pageVO.getCurPage());
		logger.debug("filterStr: " + pageVO.getFilterStr());
		return studentDao.findPagedStudentList(queryStudent, pageVO);
	}
	/**
	 * 分页查找学生信息
	 * @param queryStudent 查询条件
	 * @param pageVO 分页信息
	 * @return
	 */
	public PagedResult<StudentVO> findPagedStudentList(StudentVO queryStudent, PageVO pageVO) {
		logger.debug("Before findPagedStudentList");
		logger.debug("CurPage: " + pageVO.getCurPage());
		logger.debug("resultMode1: " + pageVO.getResultMode());
		return studentDao.findPagedStudentList(queryStudent, pageVO);
	}
	
	public PagedResult<StudentVO> findModePagedStudentList(StudentVO queryStudent, PageVO pageVO) {
		logger.debug("Before findPagedStudentList");
		logger.debug("CurPage: " + pageVO.getCurPage());
		logger.debug("resultMode1: " + pageVO.getResultMode());
		return studentDao.findPagedStudentList(queryStudent, pageVO);
	}	
	
	public PagedResult<StudentVO> findFilterModePagedStudentList(StudentVO queryStudent, PageVO pageVO) {
		return studentDao.findPagedStudentList(queryStudent, pageVO);
	}	
	/**
	 * 通过排序分页查找学生信息
	 * @param taxis 排序字段，如：age desc或者age asc
	 * @param pageVO
	 * @return
	 */
	public PagedResult<StudentVO> findPagedStudentListBySort(StudentVO queryStudent, PageVO pageVO, String taxis) {
		logger.debug("Before findPagedStudentListBySort---->"+taxis);
		return studentDao.findPagedStudentListBySort(queryStudent, pageVO, taxis);
	}
	
	/**
	 * 查询单个学生信息
	 * @param studentVO
	 * @return
	 * @throws ApplicationException
	 */
	public StudentVO findStudentByID(StudentVO studentVO) throws ApplicationException {
		return studentDao.findStudentByID(studentVO);
	}
	
	/**
	 * 通过ID查找学生信息	
	 * @param studentId
	 * @return
	 */
	public List<StudentVO> findStudentListByID(List<Integer> studentId) {
		logger.debug("Before findStudentListByID---->"+studentId.get(0));
		return studentDao.findStudentListByID(studentId);
	}
	
	/**
	 * 增加单个学生信息	
	 * @param newStudent
	 * @throws ApplicationException
	 */
	public void createStudent(StudentVO newStudent) throws ApplicationException {
		List<StudentVO> newStudents = new ArrayList<StudentVO>();
		newStudents.add(newStudent);
		self.createStudents(newStudents);
	}

	/**
	 * 删除单个学生信息
	 * @param existedStudent
	 * @throws ApplicationException
	 */
	public void deleteStudent(Integer studentId) throws ApplicationException {
		List<StudentVO> existedStudents = new ArrayList<StudentVO>();
		StudentVO student =  new StudentVO();
		student.setId(studentId);
		existedStudents.add(student);
		self.deleteStudents(existedStudents);
	}

	/**
	 * 修改单个学生信息
	 * @param existedStudent
	 * @throws ApplicationException
	 */
	public void updateStudent(StudentVO existedStudent) throws ApplicationException {
		List<StudentVO> existedStudents = new ArrayList<StudentVO>();
		existedStudents.add(existedStudent);
		self.updateStudents(existedStudents);
	}
	
	/**
	 * 批量增加学生信息
	 * @param newStudents
	 */
	public void createStudents(List<StudentVO> newStudents) throws ApplicationException {
		studentDao.createStudents(newStudents);
		logger.debug("After createStudents");
		
	}

	/**
	 * 批量删除学生信息	
	 * @param existedStudents
	 * @return
	 */
	public void deleteStudents(List<StudentVO> existedStudents) throws ApplicationException {
		DaoUtil.assertEffected(studentDao.deleteStudents(existedStudents), existedStudents.size(), MESSAGE);
		logger.debug("After deleteStudents");
		
	}

	/**
	 * 批量修改学生信息
	 * @param existedStudents
	 * @return
	 */
	public void updateStudents(List<StudentVO> existedStudents) throws ApplicationException {
		studentDao.updateStudents(existedStudents);
		logger.debug("Before updateStudents");
		
	}

	/**
	 * 批量操作学生信息
	 * @param batchItems
	 * @throws ApplicationException
	 */
	public void batchOperation(BatchVO<StudentVO> batchItems) throws ApplicationException {
		// 如果对象不为空 执行
		if(!batchItems.getItems2Create().isEmpty()){
			self.createStudents(batchItems.getItems2Create());
			logger.debug("After batchOperation createStudents");
		}
		// 如果对象不为空 执行
		if(!batchItems.getItems2Delete().isEmpty()){
			self.deleteStudents(batchItems.getItems2Delete());
			logger.debug("After batchOperation deleteStudents");
		}
		// 如果对象不为空 执行
		if(!batchItems.getItems2Update().isEmpty()){
			self.updateStudents(batchItems.getItems2Update());
			logger.debug("After batchOperation updateStudents");
		}
	}

	/**
	 * 通过学生ID集合批量删除学生信息
	 * @param studentIds
	 * @throws ApplicationException
	 */
	public void deleteStudentsBySIds(List<Integer> studentIds) throws ApplicationException {
		DaoUtil.assertEffected(studentDao.deleteStudentsBySIds(studentIds), studentIds.size(), MESSAGE);
		logger.debug("After deleteStudentsBySIds");
		
	}

	public void setSelf(Object self) {
		this.self = (IStudentService) self;
	}
	
	public PagedResult<StudentVO> findTimeList(PageVO pageVO) throws ApplicationException {
		return studentDao.findTimeList(pageVO);
	}
	@Override
	public EnumItem<HobbyEnum> testEnumItemPathParams(EnumItem<HobbyEnum> hobby) {
		logger.debug("hobby:"+hobby);
		return hobby;
	}
	
	@Override
	public StudentVO testEnumItemPathParams2(StudentVO student) {
		logger.debug("student:"+student);
		return student;
	}


}
